"""
__project__ = 'thc'
__file_name__ = 'send_email'
__author__ = 'thc'
__time__ = '2021/1/7/007 15:33'
__product_name = PyCharm
-*- coding:utf-8 -*-
"""

import smtplib                                        # 引用发生邮件模块
from email.mime.text import MIMEText                  # 编写邮件内容
from email.mime.multipart import MIMEMultipart        # 传送邮件附件
from email.header import Header                       # 编写邮件主题


class Test_mail(object):
    def test_send_email(self):
        smtpserver = 'smtp.163.com'             # 创建发生邮箱的服务器
        username = '13269120551@163.com'
        password = 'CDDEHUGOTSNVOXQV'

        sender = '13269120551@163.com'          # 发送邮箱
        receive = '2391493960@qq.com'           # 接收邮箱

        subject = '邮箱发送练习'                  # 邮件主题
        content = '123123'                      # 邮件内容

        # 设置附件
        send_file = open(r'./log.gif', 'rb').read()
        att = MIMEText(send_file, 'base64', 'utf-8')
        att['Content-Type'] = 'application/octet-stream'
        att['Content-Disposition'] = 'attachment;filename="log.gif"'

        msg = MIMEMultipart()
        msg.attach(MIMEText(content, 'utf-8'))
        msg['Subject'] = Header(subject, 'utf-8')
        msg['FROM'] = sender
        msg['TO'] = receive
        msg.attach(att)

        smtp = smtplib.SMTP_SSL(smtpserver,994)
        smtp.helo(smtpserver)
        smtp.ehlo(smtpserver)
        smtp.login(username, password)

        print('开始')
        smtp.sendmail(sender, receive, msg.as_string())
        smtp.quit()
        print('结束')


if __name__ == '__main__':
    email = Test_mail()
    email.test_send_email()
